//
//  HDNetWorking.m
//  ICBCSuisseCredit
//
//  Created by Victor on 2017/1/6.
//  Copyright © 2017年 Victor. All rights reserved.
//

#import "HFNetWork.h"
#import <UIKit+AFNetworking.h>
#import <UIKit+AFNetworking.h>
/*!@brief ****  登录接口  */
NSString * Login = @"login";
/*!@brief ****   升级提示 */
NSString * Version = @"version";

static AFSecurityPolicy * securityPolicy = nil;
static AFHTTPSessionManager *manager;
@implementation HFNetWork

+(AFHTTPSessionManager *)sharedHttpSessionManager {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        manager = [AFHTTPSessionManager manager];
    });
    
    return manager;
}

+(void)postURL:(NSString *)URL withParameters:(NSDictionary *)parameters withOperationCancel:(BOOL)isCancel Success:(void(^)(NSDictionary *result))success failure:(void(^)(NSDictionary *error))failure
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithDictionary:parameters];
   [self POST:URL parameters:dict withOperation:isCancel success:success failure:failure];
}

//post 请求
+ (void)POST:(NSString *)URLString parameters:(id)parameters withOperation:(BOOL)isCancel success:(void (^)(NSDictionary *))success failure:(void (^)(NSDictionary *))failure
{
    
    AFHTTPSessionManager *manager = [HFNetWork sharedHttpSessionManager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = 30;//请求超时时间,afn默认60s

    //    manager.securityPolicy = [HDNetWorking customSecurityPolicy];
    if([URLString rangeOfString:@"version"].location != NSNotFound)
        /*!@brief ****   版本更新 */
    {
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        
    }else
    {/*!@brief ****   非版本更新 */
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
    }
    
    
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain", @"multipart/form-data", @"application/json", @"text/html", @"image/jpeg", @"image/png", @"application/octet-stream", @"text/json", nil];
    
    
            URLString = [HOST stringByAppendingString:URLString];
        
    
    //TODO::断网状态提示
    AFNetworkReachabilityManager * reachManager = [AFNetworkReachabilityManager sharedManager] ;
    [reachManager startMonitoring];
    [reachManager setReachabilityStatusChangeBlock:^(AFNetworkReachabilityStatus status) {
        
        if(status == AFNetworkReachabilityStatusUnknown || status == AFNetworkReachabilityStatusNotReachable){
            [[DYToastLaebl share] showMessage:@"请连接网络" WithTime:3];
        }
    }];
    
    [manager POST:URLString parameters:parameters progress:^(NSProgress * _Nonnull uploadProgress){
        // 这里可以获取到目前数据请求的进度
        NSLog(@"数据进度:uploadProgress:\n%@\n",uploadProgress);
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {

        NSLog(@"成功");
        NSError * error = nil;
        NSDictionary *resultDictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];

        success(resultDictionary);
        failure(resultDictionary);
        
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"网络请求失败");
    }];
}

+(void)getURL:(NSString *)URL withParameters:(NSDictionary *)parameters withOperationCancel:(BOOL)isCancel Success:(void(^)(NSDictionary *result))success failure:(void(^)(NSDictionary *error))failure;
{
    AFHTTPSessionManager *manager = [HFNetWork sharedHttpSessionManager];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    manager.requestSerializer.timeoutInterval = 30;//请求超时时间,afn默认60s
    
    //    manager.securityPolicy = [HDNetWorking customSecurityPolicy];
    if([URL rangeOfString:@"version"].location != NSNotFound)
    /*!@brief ****   版本更新 */
    {
        manager.requestSerializer = [AFHTTPRequestSerializer serializer];
        
    }else
    {/*!@brief ****   非版本更新 */
        manager.requestSerializer = [AFJSONRequestSerializer serializer];
    }
    
    
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/plain", @"multipart/form-data", @"application/json", @"text/html", @"image/jpeg", @"image/png", @"application/octet-stream", @"text/json", nil];
    
    
    URL = [HOST stringByAppendingString:URL];
    
    [manager GET:URL parameters:parameters progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSLog(@"成功");
        NSError * error = nil;
        NSDictionary *resultDictionary = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableLeaves error:&error];
        
        success(resultDictionary);
        failure(resultDictionary);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"网络请求失败");
    }];
}

@end
